package cn.jluzh.blog.mapper;

import cn.jluzh.blog.modules.tag.entity.SysTag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysTagMapper extends BaseMapper<SysTag> {

    @Select(" SELECT vt.*, count(vat.tag_id) as articles from sys_tag vt\n" +
            "        LEFT JOIN sys_article_tag vat on vt.id = vat.tag_id\n" +
            "        WHERE vat.tag_id = #{id}")
    SysTag getTagById(Integer id);


    @Select("SELECT vt.* from sys_article_tag vat INNER JOIN sys_tag vt ON vat.tag_id=vt.id where vat.article_id=#{articleId}")
    List<SysTag> getTagByArtId(long articleId);

    @Select("SELECT vt.*, count(vat.tag_id) as articles from sys_tag vt\n" +
            "        LEFT JOIN sys_article_tag vat on vt.id = vat.tag_id\n" +
            "        GROUP BY vt.id")
    List<SysTag> getAllTag();

    @Select("select tag_id from sys_article_tag GROUP BY tag_id ORDER BY count(*) desc\n" +
            "       limit 0,#{limit}")
    List<Integer> queryHotTagIds(Integer limit);

    @Select("select * from sys_tag where tag_name like CONCAT('%',#{query},'%')")
    List<SysTag> getAllTagByName(String query);
}
